﻿Imports System
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Media.Imaging
Imports System.Windows.Navigation
Imports System.Windows.Shapes
Imports OMANE.Modules.TauxGarantis.ViewModels
Imports OMANE.Common.UI

Namespace Views
    Partial Public Class ViewCalculsDetailles
        Public Sub New()
            MyBase.New()

            Me.InitializeComponent()

            ' Insert code required on object creation below this point.
        End Sub

        Private Sub GroupExpander_Collapsed(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
            'TODO: Add event handler implementation here.

            'GroupRow.Height = GridLength.Auto


            RefreshExpanders()
        End Sub
        Private Sub OriginalExpander_Collapsed(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
            ''TODO: Add event handler implementation here.
            'OriginalRow.Height = GridLength.Auto
            RefreshExpanders()
        End Sub

        Private Sub GroupExpander_Expanded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
            ''TODO: Add event handler implementation here.
            'OriginalRow.Height = New GridLength()
            'GroupRow.Height = New GridLength()
            RefreshExpanders()
        End Sub
        Private Sub OriginalExpander_Expanded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
            ''TODO: Add event handler implementation here.
            'GroupRow.Height = New GridLength()
            'OriginalRow.Height = New GridLength()
            RefreshExpanders()
        End Sub
        Dim previousstatus As Integer = 0
        Private Sub RefreshExpanders()
            Dim status As Integer = _
                If(GroupExpander.IsExpanded, 1, 0) * 10 + _
                If(OriginalExpander.IsExpanded, 1, 0)

            Select Case status
                Case 0
                    GroupRow.Height = GridLength.Auto
                    OriginalRow.Height = GridLength.Auto
                Case 10

                    GroupRow.Height = New GridLength(1, GridUnitType.Star)
                    OriginalRow.Height = GridLength.Auto
                Case 1
                    GroupRow.Height = GridLength.Auto
                    OriginalRow.Height = New GridLength(1, GridUnitType.Star)
                Case 11
                    OriginalRow.Height = New GridLength(1, GridUnitType.Star)
                    GroupRow.Height = New GridLength(1, GridUnitType.Star)
            End Select

            previousstatus = status
        End Sub
        Private ss As Boolean = False
        Private su As Boolean = False
        Private Sub GroupGrd_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs)
            If su Or ss Then
                Dim row As DataRowView = TryCast(GroupGrd.SelectedItem, DataRowView)
                Dim vm As ViewModelCalculsDetailles = TryCast(DataContext, ViewModelCalculsDetailles)

                If row IsNot Nothing And vm IsNot Nothing Then
                    '    'OriginalGrd.ItemsSource =

                    '    'Dim q = From e In row.CreateChildView("GroupToLink")
                    '    '        Select 

                    '    'Dim q = From o In vm.OriginalTable, g In vm.GroupTable
                    '    '        Where g Is row.Row
                    '    '        Join l In vm.LinkTable On l("GroupID") Equals o("GroupID") And l("SourceID") Equals g("SourceID")
                    '    Dim groupID = CInt(row("GroupID"))

                    '    Dim originalview As DataView = OriginalGrd.ItemsSource

                    '    Dim linkview As New DataView(vm.LinkTable)

                    '    linkview.RowFilter = "GroupID=" & groupID


                    '    'originalview.RowFilter = "Parent(SourceToLink).GroupID=" & groupID.ToString()
                    'Else
                    '    'OriginalGrd.ItemsSource = DataContext.OriginalTable

                    ' Dim q = From o In vm.OriginalTable, g In vm.GroupTable
                    '          Where g Is row.Row
                    'Join l In vm.LinkTable On l("GroupID") Equals o("GroupID") And l("SourceID") Equals g("SourceID")

                    Dim groupID = CInt(row("GroupID"))
                    Dim q = From elt As DataRow In row.Row.GetChildRows("GroupToLink")
                            Select CStr(elt("SourceID"))

                    Dim tot = String.Join(",", q.ToArray())

                    Dim originalview As DataView = OriginalGrd.ItemsSource

                    If ss Then
                        originalview.RowFilter = "SourceID IN (" & tot & ")"
                    ElseIf su Then
                        originalview.RowFilter = "Not(SourceID IN (" & tot & "))"
                    End If
                    'Dim r = q.ToList()

                    'OriginalGrd.ItemsSource = r
                End If
            End If

        End Sub

        Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
            If OriginalGrd.SelectedItems Is Nothing Then Return

            Dim q = Aggregate elt As DataRowView In OriginalGrd.SelectedItems
                    Into Sum(CDbl(elt("Weight")))

            Dim vm As ViewModelCalculsDetailles = TryCast(DataContext, ViewModelCalculsDetailles)

            Dim r = vm.GroupTable.NewRow()

            r(1) = q

            'Command="{Binding GroupCommand, Mode=OneWay}"
            For Each elt In OriginalGrd.SelectedItems
                Try
                    vm.LinkTable.Rows.Add(r(0), elt("SourceID"), 1)
                Catch ex As Exception

                End Try
            Next
        End Sub



        Private Sub MenuItem1_Click_1(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
            If OriginalGrd.SelectedItems Is Nothing Then Return

            Dim q = Aggregate elt As DataRowView In OriginalGrd.SelectedItems
                    Into Sum(CDbl(elt("Weight")))

            Dim vm As ViewModelCalculsDetailles = TryCast(DataContext, ViewModelCalculsDetailles)

            Dim weightcol As String = If(vm.OriginalTable.Columns.Contains("CSR"), "CSR", "Weight")

            Dim r = vm.GroupTable.NewRow()

            Dim qtot = Aggregate elt As DataRowView In OriginalGrd.SelectedItems Into Sum(CDbl(elt.Row(weightcol)))

            For i As Integer = 0 To vm.GroupTable.Columns.Count - 1
                Dim col As DataColumn = vm.GroupTable.Columns(i)
                If col.ColumnName = "GroupID" Or _
                    col.ColumnName = "SumOfWeight" Or _
                    col.ColumnName = "CSR" Then
                Else
                    If col.DataType.IsValueType Then

                        Dim q3 = Aggregate elt As DataRowView In OriginalGrd.SelectedItems Into Sum(CDbl(elt.Row(weightcol)) * CDbl(elt.Row(col.ColumnName)))

                        r(col.ColumnName) = q3 / qtot
                    Else
                        Dim q2 = From elt As DataRowView In OriginalGrd.SelectedItems
                                 Select CStr(elt.Row(col.ColumnName)) Distinct

                        r(col.ColumnName) = String.Join(vbCrLf, q2.ToArray())
                    End If
                End If

            Next

            r("SumOfWeight") = q

            vm.GroupTable.Rows.Add(r)
            'Command="{Binding GroupCommand, Mode=OneWay}"
            For Each elt In OriginalGrd.SelectedItems
                Try
                    vm.LinkTable.Rows.Add(r("GroupID"), elt("SourceID"), 1)
                Catch ex As Exception
                    Dim k = 1
                End Try
            Next

            If ShowNeverSelected.IsChecked.Value Then
                ShowNeverSelected_Checked(Nothing, Nothing)
            ElseIf ShowSelected.IsChecked.Value Then
                ShowSelected_Checked(Nothing, Nothing)
            End If

        End Sub

        Private Sub ShowAll_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles ShowAll.Checked
            Dim vm As ViewModelCalculsDetailles = TryCast(DataContext, ViewModelCalculsDetailles)
            If vm IsNot Nothing And OriginalGrd IsNot Nothing Then
                Dim originalview As DataView = OriginalGrd.ItemsSource

                originalview.RowFilter = Nothing
            End If
            ss = False
            su = False
        End Sub

        Private Sub ShowSelected_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles ShowSelected.Checked
            ss = True
            su = False
            GroupGrd_SelectionChanged(Nothing, Nothing)
        End Sub

        Private Sub ShowUnselected_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles ShowUnselected.Checked
            ss = False
            su = True
            GroupGrd_SelectionChanged(Nothing, Nothing)
        End Sub

        Private Sub MenuItem1_Click_2(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
            Dim row As DataRowView = TryCast(GroupGrd.SelectedItem, DataRowView)
            Dim vm As ViewModelCalculsDetailles = TryCast(DataContext, ViewModelCalculsDetailles)

            If row Is Nothing Or vm Is Nothing Then Return

            Dim groupID = CInt(row("GroupID"))

            For Each r In row.Row.GetChildRows("GroupToLink")
                vm.LinkTable.Rows.Remove(r)
            Next

            vm.GroupTable.Rows.Remove(row.Row)
        End Sub

        Private Sub GroupGrd_AutoGeneratingColumn(ByVal sender As Object, ByVal e As System.Windows.Controls.DataGridAutoGeneratingColumnEventArgs) Handles GroupGrd.AutoGeneratingColumn

            If e.PropertyName = "GroupToLink" Then
                e.Cancel = True
            End If
        End Sub

        Private Sub OriginalGrd_AutoGeneratingColumn(ByVal sender As Object, ByVal e As System.Windows.Controls.DataGridAutoGeneratingColumnEventArgs) Handles OriginalGrd.AutoGeneratingColumn
            If e.PropertyName = "SourceToLink" Then
                e.Cancel = True
            End If
        End Sub

        Private Sub LinkGrd_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles LinkGrd.SelectionChanged

        End Sub

        Private Sub MenuItem1_Click_3(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MenuItem1.Click
            If LinkGrd.SelectedItems Is Nothing Then Return

            Dim vm As ViewModelCalculsDetailles = TryCast(DataContext, ViewModelCalculsDetailles)

            If vm Is Nothing Then Return

            ' ModuleTauxGaranti._app.GetService(Of IUIService).show
            Dim q = From r As DataRowView In LinkGrd.SelectedItems Select r.Row
            For Each r As DataRow In q.ToArray()
                vm.LinkTable.Rows.Remove(r)
            Next

            Dim obsoletegroup As New List(Of DataRow)

            For Each r As DataRow In vm.GroupTable.Rows
                If r.GetChildRows("GroupToLink").Length = 0 Then
                    obsoletegroup.Add(r)
                End If
            Next

            For Each r In obsoletegroup
                vm.GroupTable.Rows.Remove(r)
            Next
        End Sub

        Private Sub CheckBox_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
            'TODO: Add event handler implementation here.
            sender.DataContext.Visibility = Visibility.Visible
        End Sub

        Private Sub CheckBox_Unchecked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
            'TODO: Add event handler implementation here.
            sender.DataContext.Visibility = Visibility.Collapsed
        End Sub

        Private Sub ShowSelectedOne_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles ShowSelectedOne.Checked
            Dim vm As ViewModelCalculsDetailles = TryCast(DataContext, ViewModelCalculsDetailles)
            If vm IsNot Nothing And OriginalGrd IsNot Nothing Then
                Dim originalview As DataView = OriginalGrd.ItemsSource

                Dim q = From r In vm.LinkTable.Rows
                        Select CInt(r("SourceID")) Distinct

                If q.Count = 0 Then
                    originalview.RowFilter = Nothing
                Else
                    Dim tot = String.Join(",", q.ToArray())
                    originalview.RowFilter = "SourceID IN (" & tot & ")"
                End If
            End If
            ss = False
            su = False
        End Sub

        Private Sub ShowNeverSelected_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles ShowNeverSelected.Checked
            Dim vm As ViewModelCalculsDetailles = TryCast(DataContext, ViewModelCalculsDetailles)
            If vm IsNot Nothing And OriginalGrd IsNot Nothing Then
                Dim originalview As DataView = OriginalGrd.ItemsSource

                Dim q = From r In vm.LinkTable.Rows
                        Select CInt(r("SourceID")) Distinct

                If q.Count = 0 Then
                    originalview.RowFilter = Nothing
                Else
                    Dim tot = String.Join(",", q.ToArray())
                    originalview.RowFilter = "NOT(SourceID IN (" & tot & "))"
                End If
            End If
            ss = False
            su = False

        End Sub


        Private Sub FilterTextBox_TextChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.TextChangedEventArgs)
            'TODO: Add event handler implementation here.
        End Sub

        Private Sub FilterTextBox_PreviewKeyUp(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs)
            'TODO: Add event handler implementation here.
            If e.Key = Key.Enter Then
                If OriginalGrd IsNot Nothing Then
                    Dim originalview As DataView = OriginalGrd.ItemsSource
                    originalview.RowFilter = FilterTextBox.Text.Trim()
                    OriginalGrd.Focus()
                End If
            End If
            If e.Key = Key.Escape Then
                If OriginalGrd IsNot Nothing Then
                    Dim originalview As DataView = OriginalGrd.ItemsSource
                    FilterTextBox.Text = originalview.RowFilter
                    OriginalGrd.Focus()
                End If
            End If
        End Sub

        Private Sub BtnComputedColumn_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles BtnComputedColumn.Click
            Dim expr As String = InputBox("Entrer la formule")
            If String.IsNullOrWhiteSpace(expr.Trim()) Then Return
            Dim vm As ViewModelCalculsDetailles = TryCast(DataContext, ViewModelCalculsDetailles)
            If vm Is Nothing Then Return

            Dim col = vm.OriginalTable.Columns.Add("Column_Computed_" & vm.OriginalTable.Columns.Count)
            'col.DataType = GetType(Object)
            col.Expression = expr

        End Sub

      
        Private Sub modeCheckBox_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles modeCheckBox.Checked
            detailGrd.Visibility = Windows.Visibility.Collapsed
            simpleGrd.Visibility = Windows.Visibility.Visible

            Dim vm = TryCast(DataContext, ViewModelCalculsDetailles)
            If vm IsNot Nothing Then
                vm.RefreshFinalTable()
            End If
        End Sub

        Private Sub modeCheckBox_Unchecked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles modeCheckBox.Unchecked
            detailGrd.Visibility = Windows.Visibility.Visible
            simpleGrd.Visibility = Windows.Visibility.Collapsed

        End Sub

        Private Sub SimpleGrd_Grouper_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MenuItem1.Click
            If simpleDataGrid.SelectedItems Is Nothing Then Return
            If simpleDataGrid.SelectedItems.Count < 1 Then Return
            Dim vm As ViewModelCalculsDetailles = TryCast(DataContext, ViewModelCalculsDetailles)
            If vm Is Nothing Then Return

            Dim originalrows As New List(Of Integer)
            For Each rv As DataRowView In simpleDataGrid.SelectedItems
                For Each r As DataRow In rv.Row.GetChildRows("GroupToLink")
                    If Not originalrows.Contains(CInt(r("SourceID"))) Then
                        originalrows.Add(CInt(r("SourceID")))
                    End If
                Next
            Next
            For Each rv As DataRowView In simpleDataGrid.SelectedItems
                For Each r As DataRow In rv.Row.GetChildRows("GroupToLink")
                    vm.LinkTable.Rows.Remove(r)
                Next
            Next
            Dim grows As New List(Of DataRow)
            For Each r As DataRowView In simpleDataGrid.SelectedItems
                grows.Add(r.Row)
            Next
            For Each r As DataRow In grows
                vm.GroupTable.Rows.Remove(r)
            Next
            grows.Clear()
            grows = Nothing
            Dim rg = vm.GroupTable.NewRow()

            vm.GroupTable.Rows.Add(rg)

            For Each elt As Integer In originalrows
                vm.LinkTable.Rows.Add(rg("GroupID"), elt, CDbl(1))
            Next

            ReCalculate(rg)
        End Sub
        Private Sub SimpleGrd_DeGrouper_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MenuItem1.Click
            If simpleDataGrid.SelectedItems Is Nothing Then Return
            If simpleDataGrid.SelectedItems.Count < 1 Then Return
            Dim vm As ViewModelCalculsDetailles = TryCast(DataContext, ViewModelCalculsDetailles)
            If vm Is Nothing Then Return

            Dim originalrows As New List(Of Integer)
            For Each rv As DataRowView In simpleDataGrid.SelectedItems
                For Each r As DataRow In rv.Row.GetChildRows("GroupToLink")
                    If Not originalrows.Contains(CInt(r("SourceID"))) Then
                        originalrows.Add(CInt(r("SourceID")))
                    End If
                Next
            Next

            For Each rv As DataRowView In simpleDataGrid.SelectedItems
                For Each r As DataRow In rv.Row.GetChildRows("GroupToLink")
                    vm.LinkTable.Rows.Remove(r)
                Next
            Next
            Dim grows As New List(Of DataRow)
            For Each r As DataRowView In simpleDataGrid.SelectedItems
                grows.Add(r.Row)
            Next
            For Each r As DataRow In grows
                vm.GroupTable.Rows.Remove(r)
            Next
            grows.Clear()
            grows = Nothing

            
            Dim newrows As New List(Of DataRow)
            For Each elt As Integer In originalrows
                Dim rg = vm.GroupTable.NewRow()
                vm.GroupTable.Rows.Add(rg)
                newrows.Add(rg)
                vm.LinkTable.Rows.Add(rg("GroupID"), elt, CDbl(1))
            Next
            For Each r In newrows
                ReCalculate(r)
            Next
            newrows.Clear()
            newrows = Nothing
        End Sub
        Private Sub ReCalculate(ByVal groupRow As DataRow)
            Dim vm As ViewModelCalculsDetailles = TryCast(DataContext, ViewModelCalculsDetailles)

            Dim weightcol As String = If(vm.OriginalTable.Columns.Contains("CSR"), "CSR", "Weight")

            Dim qtot As Double = Aggregate r In groupRow.GetChildRows("GroupToLink")
                                 Into Sum(CDbl(r.GetParentRow("SourceToLink")(weightcol)))

            For Each col As DataColumn In vm.GroupTable.Columns
                If col.ColumnName = "GroupID" Then
                ElseIf col.ColumnName = "SumOfWeight" Then
                    groupRow(col) = groupRow.GetChildRows("GroupToLink").Length
                ElseIf col.ColumnName = weightcol Then
                    groupRow(col) = qtot
                ElseIf col.ColumnName = "POIDS_CSR" Then

                Else
                    If vm.OriginalTable.Columns.Contains(col.ColumnName) Then
                        If col.DataType.IsValueType Then
                            Dim qagg = Aggregate r In groupRow.GetChildRows("GroupToLink")
                                       Into Sum(CDbl(r.GetParentRow("SourceToLink")(weightcol)) * CDbl(r.GetParentRow("SourceToLink")(col.ColumnName)))

                            groupRow(col) = qagg / qtot
                        Else
                            Dim q2 = From r In groupRow.GetChildRows("GroupToLink")
                                     Select CStr(r.GetParentRow("SourceToLink")(col.ColumnName)) Distinct

                            groupRow(col) = String.Join(vbCrLf, q2.ToArray())
                        End If
                    End If
                End If
            Next
        End Sub

        Private Sub simpleFilterTextBox_PreviewKeyUp(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles simpleFilterTextBox.PreviewKeyUp
            If e.Key = Key.Enter Then
                If simpleDataGrid IsNot Nothing Then
                    Dim originalview As DataView = simpleDataGrid.ItemsSource
                    originalview.RowFilter = simpleFilterTextBox.Text.Trim()
                    simpleDataGrid.Focus()
                End If
            End If
            If e.Key = Key.Escape Then
                If simpleDataGrid IsNot Nothing Then
                    Dim originalview As DataView = simpleDataGrid.ItemsSource
                    simpleFilterTextBox.Text = originalview.RowFilter
                    OriginalGrd.Focus()
                End If
            End If
        End Sub

        Private Sub simpleBtnComputedColumn_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles simpleBtnComputedColumn.Click
            Dim expr As String = InputBox("Entrer la formule")

            If String.IsNullOrWhiteSpace(expr.Trim()) Then

                Return
            End If
            Dim vm As ViewModelCalculsDetailles = TryCast(DataContext, ViewModelCalculsDetailles)
            If vm Is Nothing Then Return

            Dim col = vm.FinalTable.Columns.Add("Column_Computed_" & vm.FinalTable.Columns.Count, GetType(Object), expr)

            Dim vs As New Hashtable
            For Each c In simpleDataGrid.Columns
                vs.Add(c.Header, c.Visibility)
            Next
            simpleDataGrid.Columns.Clear()
            For Each c In vm.FinalTable.Columns
                Dim gcol = New DataGridTextColumn() With {.Header = c.ColumnName, .Binding = New Binding(c.ColumnName)}
                If vs.ContainsKey(gcol.Header) Then
                    gcol.Visibility = vs(gcol.Header)
                End If
                simpleDataGrid.Columns.Add(gcol)
            Next
            vm.RefreshFinalTable()
        End Sub

        Private Sub AdvancedOptions_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles AdvancedOptions.Click
            'Dim q = From elt As DataGridBoundColumn In simpleDataGrid.Columns
            '        Select New ViewModelCalculDetailleGridColumnOption() With { _
            '            .ColumnName = elt.Header.ToString(), _
            '            .Visible = (elt.Visibility = Windows.Visibility.Visible), _
            '            .Format = elt.Binding.StringFormat}

            Dim q = From elt As DataGridBoundColumn In simpleDataGrid.Columns Select New ViewModelCalculDetailleGridColumnOption(elt)

            Dim opts As New ViewModelCalculDetailleOption(simpleDataGrid)
            opts.Columns = New ObjectModel.ObservableCollection(Of ViewModelCalculDetailleGridColumnOption)(q.AsEnumerable())

            Dim ui = ModuleTauxGaranti._app.GetService(Of IUIService)()

            ui.ShowMessage(opts)
        End Sub

        Private HideShowAllState As Boolean = True
        Private Sub HideShowAll_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles HideShowAll.Click
            HideShowAllState = Not HideShowAllState
            HideShowAll.Content = If(HideShowAllState, _
                    "Cacher toutes les colonnes", _
                    "Afficher toutes les colonnes")
            For Each elt In simpleDataGrid.Columns
                elt.Visibility = If(HideShowAllState, Windows.Visibility.Visible, Windows.Visibility.Collapsed)
            Next
        End Sub

        Private Sub simpleDataGrid_AutoGeneratedColumns(ByVal sender As Object, ByVal e As System.EventArgs) Handles simpleDataGrid.AutoGeneratedColumns
            Dim opt = New ViewModelCalculDetailleOption(simpleDataGrid)
            'Dim p = IO.File.Exists("..\..\..\03_Paramétrage\default_good_well.view")
            'Dim p1 = IO.File.Exists("..\..\..\03_Paramétrage\default_good_well.view")
            'Dim p2 = IO.File.Exists("..\..\..\..\03_Paramétrage\default_good_well.view")
            'Dim p3 = IO.File.Exists("..\..\..\..\..\03_Paramétrage\default_good_well.view")
            'Dim p4 = IO.File.Exists("..\..\..\..\..\..\03_Paramétrage\default_good_well.view")
            'Dim p5 = IO.File.Exists("..\..\..\..\..\..\..\03_Paramétrage\default_good_well.view")
            'Dim p6 = IO.File.Exists("..\..\..\..\..\..\..\..\..\03_Paramétrage\default_good_well.view")
            'Dim p7 = IO.File.Exists("..\..\..\..\..\..\..\..\03_Paramétrage\default_good_well.view")
            'Dim p8 = IO.File.Exists("..\..\..\..\..\..\..\..\..\..\03_Paramétrage\default_good_well.view")
            'Dim p9 = IO.File.Exists("..\..\..\..\..\..\..\..\..\03_Paramétrage\default_good_well.view")
            'Dim p10 = IO.File.Exists("..\..\..\..\..\..\..\..\..\..\..\03_Paramétrage\default_good_well.view")

            Dim sinfo = Application.GetResourceStream( _
                New Uri("/OMANE.Modules.TauxGarantis;component/default_good_well.view", System.UriKind.Relative))

            opt.LoadParametresExecute(sinfo.Stream)
        End Sub
    End Class
End Namespace


Public Class NewColumnData

End Class

Public Class DataRowViewConverter
    Inherits DependencyObject
    Implements IValueConverter



    Public Property Column As DataGridColumn
        Get
            Return GetValue(ColumnProperty)
        End Get

        Set(ByVal value As DataGridColumn)
            SetValue(ColumnProperty, value)
        End Set
    End Property

    Public Shared ReadOnly ColumnProperty As DependencyProperty = _
                           DependencyProperty.Register("Column", _
                           GetType(DataGridColumn), GetType(DataRowViewConverter), _
                           New FrameworkPropertyMetadata(Nothing))



    Public Property Relation As String
        Get
            Return GetValue(RelationProperty)
        End Get

        Set(ByVal value As String)
            SetValue(RelationProperty, value)
        End Set
    End Property

    Public Shared ReadOnly RelationProperty As DependencyProperty = _
                           DependencyProperty.Register("Relation", _
                           GetType(String), GetType(DataRowViewConverter), _
                           New FrameworkPropertyMetadata(Nothing))

    Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
        Dim rv As DataRowView = value
        If Column Is Nothing Or String.IsNullOrEmpty(Relation) Then
            Return rv(parameter)
        Else
            Return rv.Row.GetParentRow(Relation)(CStr(Column.Header))
        End If
    End Function
    Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
        Throw New NotImplementedException()
    End Function
End Class
